package com.yuchen.ishop.web.app;

import com.yuchen.core.MyPager;
import com.yuchen.ishop.entity.Shop;
import com.yuchen.ishop.service.goods.GoodsRestClient;
import com.yuchen.ishop.service.seller.SellerRestClient;
import com.yuchen.ishop.service.seller.ShopRestClient;
import com.yuchen.ishop.service.seller.XiaoerRestClient;
import com.yuchen.ishop.web.BaseAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 门店
 */
@Controller
@RequestMapping(value = "/shop")
public class HisShopAction extends BaseAction {


    @Autowired
    private SellerRestClient sellerRestClient;

    @Autowired
    private ShopRestClient shopRestClient;

    @Autowired
    private GoodsRestClient goodsRestClient;

    @Autowired
    private XiaoerRestClient xiaoerRestClient;

    @RequestMapping(value = {"index"})
    public String index(Long shopId, ModelMap modelMap, HttpServletRequest request) {
        modelMap.addAttribute("shopNav","index");
        if (shopId == null) return "redirect:/error.htm";
        Shop shop = shopRestClient.get(shopId);
        if (shop != null && shop.getSellerId() != null) {
            Map params = convertParameterMap(request.getParameterMap());
            if(params==null) params = new HashMap();
            params.put("sellerId",shop.getSellerId());
            MyPager myPager = goodsRestClient.finds(params);
            modelMap.addAttribute("shop",shop);
            modelMap.addAttribute("pager",myPager);

            //xiaoer
            Map cnds = new HashMap(2);
            cnds.put("sellerId",shop.getSellerId());
            cnds.put("pageSize",5);
            modelMap.addAttribute("xiaoerPager",xiaoerRestClient.findXiaoers(cnds));
        }
        return "shop/index";
    }

    @RequestMapping(value = {"xiaoer"})
    public String xiaoer(Long shopId,ModelMap modelMap, HttpServletRequest request) {
        modelMap.addAttribute("shopNav","xiaoer");
        if (shopId == null) return "redirect:/error.htm";
        Shop shop = shopRestClient.get(shopId);
        if (shop != null && shop.getSellerId() != null) {
            modelMap.addAttribute("shop",shop);
            //xiaoer
            Map cnds = new HashMap(1);
            cnds.put("sellerId",shop.getSellerId());
            modelMap.addAttribute("xiaoerPager",xiaoerRestClient.findXiaoers(cnds));
        }
        return "shop/xiaoer";
    }

    @RequestMapping(value = {"info"})
    public String info(Long shopId,ModelMap modelMap, HttpServletRequest request) {
        modelMap.addAttribute("shopNav","info");
        if (shopId == null) return "redirect:/error.htm";
        Shop shop = shopRestClient.get(shopId);
        if (shop != null && shop.getSellerId() != null) {
            modelMap.addAttribute("shop",shop);
        }
        return "shop/info";
    }
}
